package com.zwbckmy.getAllZogs;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class GetAllZogs {

	public static void main(String[] args) throws IOException {
		// Get All Zogs From http://www.zillions-of-games.com
		String showurl = "http://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id=";
		
		for(int i=1;i<10000;i++)
    	{
    		String surl = "http://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=show;id="+String.valueOf(i);
        	
    		String downurl = "http://www.zillions-of-games.com/cgi-bin/zilligames/submissions.cgi?do=download;id="+String.valueOf(i);
    		URL url = new URL(showurl+String.valueOf(i));
    		String urlsource = getURLSource(url);
    		Document doc = Jsoup.parse(urlsource, "utf-8");
    		Elements gamename = doc.getElementsByClass("gamename");
    		String zogname = gamename.text();
    		if(!"".equals(zogname))
    		{
    			String zname = getName(zogname,i);
        		String cond = "a[class=properties]a[href="+downurl+"]";
        		Elements links = doc.select(cond);
            	String downloadlink = links.attr("onmouseover");
            	String downloadlink1 = downloadlink.substring(21, downloadlink.length()-2);
            	String dir = "D://zogdownload";
            	
            	FileUtils.copyURLToFile(new URL(downloadlink1), new File(dir,zname+".zip"));
            	
            	String creator = doc.getElementsByClass("linkup_dflt").text();
            	List<Element> tds = doc.getElementsByTag("td");
            	System.out.println(tds);
            	String t = "";
            	for(int m=0;m<tds.size();m++)
            	{
            		if(tds.get(m).text().startsWith("Created"))
            		{
            			t = tds.get(m).text();
            		}
            	}
            	
            	int p = t.indexOf(",");
            	String time = t.substring(p+1);
            	
            	System.out.println("*****ID:"+String.valueOf(i)+",Name:"+zname+",Creator:"+creator+",DownloadLink:"+downloadlink1+",ShowLink:"+surl+",Time:"+time);
            	//String[] contents = {String.valueOf(i), zname,creator, downloadlink1, surl, time};
            	//r.writeRecord(contents);
				//r.flush();
    		}
    		else
    		{
    			continue;
    		}
    	}
 	       
    }

	 /**
	 * 根据URL获得网页源码
	 * @param url 传入的URL
	 * @return String
	 * @throws IOException 
	 */
	public static String getURLSource(URL url) throws IOException {
		HttpURLConnection conn = (HttpURLConnection)url.openConnection();
	
		conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
        conn.setRequestProperty("Content-type", "application/json"); 
		
		conn.setRequestMethod("GET");
		
		conn.setConnectTimeout(500000);//时间可以设置的久一点，如果控制台经常提示read time out
		InputStream inStream = conn.getInputStream();
		byte[] data = readInputStream(inStream);
		String htmlSource = new String(data);
		return htmlSource;
	}
	
	/**
	 * 转换字节流
	 * @param instream
	 * @return
	 * @throws IOException
	 */
	public static byte[] readInputStream(InputStream instream) throws IOException {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        byte[]  buffer = new byte[1204];  
        int len = 0;  
        while ((len = instream.read(buffer)) != -1){  
            outStream.write(buffer,0,len);  
        }  
        instream.close();  
        return outStream.toByteArray(); 
	}
	
	/**
	 * 获取下载文件名称 并去掉名称中非法字符
	 * @param zogname i
	 * @return name(id+zogname)
	 * @throws 
	 */
	public static String getName(String zogname, int i) {
		// TODO Auto-generated method stub
		String name = zogname.substring(5);
		String rst = "";
		for(int j=0;j<name.length();j++)
		{//文件名、目录名或卷标语法不准确，可能是由于文件名太长或包含了这些字符“/”、“\”，“:”、“*”、“?”、“<”、“>”、“””、“|”造成的
			if(name.charAt(j)!='?'&&name.charAt(j)!='\\'&&name.charAt(j)!='/'&&name.charAt(j)!=':'&&name.charAt(j)!='|'&&name.charAt(j)!='*'&&name.charAt(j)!='<'&&name.charAt(j)!='>'&&name.charAt(j)!='"')
			{
				rst += name.charAt(j);
			}
			
		}
		return i+"_"+rst;
	}

    	

	

}

